﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Inventario_HardSoft.Controlador;
using Inventario_HardSoft.ADO;

namespace Inventario_HardSoft.Presentacion.Importar
{
    public partial class ImportarProgramas : Form
    {
        Ctrl_ImportarProgramas controlador;
        Ctrl_Prog controlPrograma;

        
        public ImportarProgramas()
        {
            InitializeComponent();
            controlador = new Ctrl_ImportarProgramas();
            controlPrograma = new Ctrl_Prog();
        }

        private void button1_Click(object sender, EventArgs e)  //boton examinar
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Documento Excel 2003 (.xls)|*.xls|Documento Excel 2007 y posterior (.xlsx)|*.xlsx";
            openDialog.FilterIndex = 1;            

            if (openDialog.ShowDialog(this) == DialogResult.OK)
            {
                textBox1.Text = openDialog.FileName;

                controlador.getTablefromExcel(dataGridView1, textBox1.Text);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //obtenemos la información del datagridview
            if (dataGridView1.Rows.Count > 0)
            {
                foreach (DataGridViewRow dr in dataGridView1.Rows)
                {
                    Programa prog = new Programa()
                    {
                        nombreprograma = dr.Cells[0].Value.ToString(),
                        fecha = DateTime.Now,
                        eliminado = 0
                    };

                    DialogResult result = controlPrograma.IngresarPrograma(prog, 1);                    

                    if (result == DialogResult.Cancel)
                    {
                        MessageBox.Show("Ocurrió un error al registrar un programa, se detiene el proceso", "Camilo", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                        break;
                    }
                }

                MessageBox.Show("La lista de programas se registró correctamente", "Camilo", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                this.Close();
            }
        }
    }
}
